<?php

namespace app\api\controller;

use app\admin\model\GoodsCollect;
use app\admin\model\GoodsOrder;
use app\common\controller\Api;
use app\admin\model\User;


/**
 * 我的接口
 */
class My extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     *我的--订单状态
     *
     */
    public function order()
    {
        //status:1=待发货,2=待收货,3=已完成
        //type:1=拼团订单,2=积分订单
        $status = input('status/d');
        $model = new GoodsOrder();
        $where['goods_order.user_id'] = $this->auth->id;
        if (!empty($status)) $where['goods_order.status'] = $status;
        $data = $model->where($where)->with(['goods'])->select();
        foreach ($data as $k => $v) {
            if (!empty($v['goods']['image'])) $data[$k]['goods']['image'] = config("url_domain_root") . $v['goods']['image'];
        }
        $this->success('获取成功', $data);
    }

    /**
     *  个人信息展览区--详情
     *
     */
    public function index()
    {
        $data = User::field('id,avatar,nickname,real_name,idcard,mobile,level_id,invitation_code,prevtime,logintime,jointime')->where('id', $this->auth->id)->find();
        if (!empty($data['avatar'])) {
            $data['avatar'] = strtolower(config("url_domain_root") . str_replace('\\', '/', $data['avatar']));
        } else {
            $data['avatar'] = config("url_domain_root") . 'avatar.png';
        }
        $data['is_real'] = 0;
        if (!empty($data['idcard']) && !empty($data['real_name'])) {
            //  用户已实名认证
            $data['is_real'] = 1;
        }
        $data['level_id'] = $data->level->title;
        unset($data->level);
        $this->success('获取成功', $data, 1);
    }

    /**
     *  修改头像+昵称
     */
    public function update()
    {
        // 接收当前用户id
        $model = new User();
        $data = input('post.');
        if (empty($data)) {
            $this->error('请选择要修改的模块！', [], 400);
        }
        // 上传文件
        if ($files = $_FILES) {
            $imageArr = array();
            foreach ($files as $file) {
                $imageName = $file['name'];
                //后缀名
                $ext = strtolower(substr(strrchr($imageName, '.'), 1));
                //保存文件名
                $fileName = uniqid();
                $tmp = $file['tmp_name'];
                //保存 = 路径 + 文件名 + 后缀名
                $imageSavePath = '../public/uploads/images/' . $fileName . '.' . $ext;
                $info = move_uploaded_file($tmp, $imageSavePath);

                if ($info) {
                    $path = "/uploads/images/" . $fileName . '.' . $ext;
                    array_push($imageArr, $path);
                } else {
                    $this->error(400, '上传失败！');
                }
            }
            $data['avatar'] = implode(',', $imageArr);
        }
        if ($model->where('id', $this->auth->id)->update($data)) {
            $this->success('修改成功！', [], 1);
        }
        $this->error('修改失败！', [], 500);

    }
}
